package com.xx.redBook.service.impl;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import com.xx.common.utils.DateUtils;
import com.xx.common.utils.SecurityUtils;
import com.xx.common.utils.poi.ExcelUtil;
import com.xx.redBook.domain.RedBookAccount;
import com.xx.redBook.mapper.RedBookAccountMapper;
import com.xx.redBook.service.IRedBookAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;

/**
 * 哔哩账号Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-28
 */
@Service
public class RedBookAccountServiceImpl implements IRedBookAccountService
{
    @Autowired
    private RedBookAccountMapper biLiAccountMapper;

    /**
     * 查询哔哩账号
     *
     * @param id 哔哩账号主键
     * @return 哔哩账号
     */
    @Override
    public RedBookAccount selectRedBookAccountById(String id)
    {
        return biLiAccountMapper.selectRedBookAccountById(id);
    }

    /**
     * 查询哔哩账号列表
     *
     * @param biLiAccount 哔哩账号
     * @return 哔哩账号
     */
    @Override
    public List<RedBookAccount> selectRedBookAccountList(RedBookAccount biLiAccount)
    {
        biLiAccount.setCreateBy(SecurityUtils.getUserId().toString());
        return biLiAccountMapper.selectRedBookAccountList(biLiAccount);
    }

    /**
     * 新增哔哩账号
     *
     * @param biLiAccount 哔哩账号
     * @return 结果
     */
    @Override
    public int insertRedBookAccount(RedBookAccount biLiAccount)
    {
        biLiAccount.setId(UUID.randomUUID().toString().replaceAll("-",""));
        biLiAccount.setCreateTime(DateUtils.getNowDate());
        biLiAccount.setCreateBy(SecurityUtils.getUserId().toString());
        biLiAccount.setCount(0);
        return biLiAccountMapper.insertRedBookAccount(biLiAccount);
    }

    /**
     * 修改哔哩账号
     *
     * @param biLiAccount 哔哩账号
     * @return 结果
     */
    @Override
    public int updateRedBookAccount(RedBookAccount biLiAccount)
    {
        return biLiAccountMapper.updateRedBookAccount(biLiAccount);
    }

    /**
     * 批量删除哔哩账号
     *
     * @param ids 需要删除的哔哩账号主键
     * @return 结果
     */
    @Override
    public int deleteRedBookAccountByIds(String[] ids)
    {

        return biLiAccountMapper.deleteRedBookAccountByIds(ids);
    }

    @Override
    public List<String> checkRedBookAccount() {
        //查看个人信息接口
        String infoUrl = "https://api.bilibili.com/x/member/web/account?web_location=333.33";

        // 查询账号列表
        List<RedBookAccount> list = selectRedBookAccountList(new RedBookAccount());

        for (RedBookAccount biLiAccount : list) {
            Map<String, String> headers = new HashMap<>();
            headers.put("Cookie", biLiAccount.getCookie());

            // 发送请求
            String result = HttpUtil.createGet(infoUrl).headerMap(headers, true).execute().body();
            JSONObject json = new JSONObject(result);
            int code = json.getInt("code");

            if(code == 0){
                biLiAccount.setLoginStatus("在线");
            }else {
                biLiAccount.setLoginStatus("离线");
            }

            biLiAccountMapper.updateRedBookAccount(biLiAccount);
        }

        return Collections.emptyList();
    }

    @Override
    public String importRedBookAccount(MultipartFile file) {
        try {
            ExcelUtil<RedBookAccount> util = new ExcelUtil<RedBookAccount>(RedBookAccount.class);
            List<RedBookAccount> list = util.importExcel(file.getInputStream());
            for (RedBookAccount biLiAccount : list) {
                insertRedBookAccount(biLiAccount);
            }
        } catch (IOException e) {
            throw new RuntimeException("导入失败，失败原因：" + e.getMessage());
        }

        return "导入成功";
    }
}
